var BlogCon = iClass("Blog");
var UserCon = iClass.get("User");
var db = iClass.db;
iClass.method("Admin", {
	//获取博文列表
	getBlogList: function(type, num, page) {
		var blog_list = db.find_all(BlogCon.dbname).filter(function(blog_info) {
			if (type) {
				return blog_info.type == type && blog_info;
			} else {
				return blog_info;
			}
		});
		//处理参数
		var total_num = blog_list.length;
		num = ~~num;
		num > 0 || (num = total_num); //num > 0
		page = ~~page;
		page >= 0 || (page = 0); //page >= 0
		var total_page = Math.ceil(total_num / num) || 1;

		//整理数据
		blog_list = blog_list.reverse().page(num, page);

		blog_list = blog_list.map(function(blog_info) {
			var blog = BlogCon.getInstance(blog_info).clone();
			blog.user = UserCon.get(blog.user_id);
			return blog;
		});
		return {
			total_num: total_num,
			total_page: total_page,
			num: num,
			page: page,
			list: blog_list
		};
	},
	//获取指定博文，以及它的操作权
	getBlog: function(blog_id) {
		var blog = BlogCon.get(blog_id);
		if (!blog) {
			throw "找不到指定博文"
		}
		return blog;
	},
	removeBlog: function(blog_id) {
		var blog = this.getBlog(blog_id);
		blog.remove();
	}
});